package designpattern.inversion;

interface IReceiver {
    String getInfo();
}

/**
 * @author study
 * @version 1.0
 * @date 2020/12/22 10:46
 */
public class DependecyInversion {
    public static void main(String[] args) {
        final Email email = new Email();
        final Person person = new Person();
        person.receive(email);

        final WeiXin weiXin = new WeiXin();
        person.receive(weiXin);

    }
}

class WeiXin implements IReceiver {
    @Override
    public String getInfo() {
        return "接受微信消息:hello";
    }
}

class Email implements IReceiver {
    public String getInfo() {
        return "电子邮件：hello,world";
    }
}

/**
 * 依赖倒转
 * <p>
 * 依赖传递的三种方式
 * 1 接口传递
 * 2 构造方法传递
 * 3 setter方式
 */

/**
 * 依赖倒转注意
 * 1 底层模块尽量都要有抽象类
 * 2 变量的声明类型尽量是抽象类或接口，
 * 3 继承时遵循里氏替换原则
 */
class Person {
    public void receive(IReceiver receiver) {
        System.out.println(receiver.getInfo());
    }
}
/**
 * 里氏替换原则
 * 之类尽量不要重写父类的方法。
 */


